package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.UserStatusActivity;
import com.github.postsanf.yinian.bean.YNMember;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MembersHorizontalAdapter extends RecyclerView.Adapter<MembersHorizontalAdapter.ViewHolder> {
    private Context mContext;
    private List<YNMember> datas;
    private int mGroupid;
    public List<String> uid = new ArrayList<>();
    public List<String> nickName = new ArrayList<>();

    public MembersHorizontalAdapter(Context mContext, List<YNMember> datas, int mGroupid) {
        this.mContext = mContext;
        this.datas = datas;
        this.mGroupid = mGroupid;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_item_album_member_avatar;                        //成员头像

        public ViewHolder(View itemView) {
            super(itemView);
            iv_item_album_member_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_item_album_member_avatar);
        }

        public void bindData(final YNMember listItems) {
            if (listItems != null) {
                if (listItems.getUser().getUpic().equals("空")) {
                    iv_item_album_member_avatar.setImageResource(-1);
                } else {
                    Uri avatarUri = Uri.parse(listItems.getUser().getUpic() + StringUtils.getQulity());
                    ImageOptHelper.showFrescoAvatar(avatarUri, iv_item_album_member_avatar, mContext);
                    iv_item_album_member_avatar.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, UserStatusActivity.class);
                            intent.putExtra(CommonConstants.YNUSERID, listItems.getUser().getUserid());
                            intent.putExtra(CommonConstants.YNGROUPID,  ((BaseApplication) mContext.getApplicationContext()).getCurGid());
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    private YNMember getItem(int position) {
        return datas.get(position);
    }


    // 创建ViewHolder
    @Override
    public MembersHorizontalAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_horizontal_member, parent, false);
        return new MembersHorizontalAdapter.ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(MembersHorizontalAdapter.ViewHolder holder, int position) {
        YNMember listItems = getItem(position);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

}
